﻿layui.use(["form", "table", "layer"], function () {
    var $ = layui.$,
        form = layui.form,
        table = layui.table,
        layer = layui.layer;
    var layeritem = null,
        tablekeywordsitem = null,
        parentid = null;


    //初始化关键词table
    var tablekeywords = table.render({
        elem: '#tablekeywords',
        url: '/Ashx/wechat.ashx', //数据接口
        method: 'post',
        where: { functionname: "jhy_wechat_keywords_query", kw_keyword: "" },
        cols: [[ //表头
            { field: 'kw_keyword', title: '关键词', edit: "text", minWidth: 120 },
            { field: 'kw_description', title: '关键词', edit: "text", minWidth: 120 },
            {
                field: 'kw_msgtype', title: '回复类型', event: "msgtype", style: " cursor:pointer", minWidth: 60, align: "center", templet: function (d) {
                    switch (d.kw_msgtype) {
                        case "text": return "文本"; break;
                        case "news": return "图文"; break;
                        case "image": return "图片"; break;
                        case "voice": return "语音"; break;
                        case "video": return "视频"; break;
                        case "music": return "音乐"; break;
                        default: return "未定义"; break;
                    }
                }
            },
            {
                field: 'kw_visable', title: '状态', event: "visable", style: " cursor:pointer", minWidth: 60, align: "center", templet: function (d) {
                    switch (d.kw_visable) {
                        case "0": return "显示"; break;
                        default: return "隐藏"; break;
                    }
                }
            },
            { field: 'kw_order', title: '排序', width: 60, align: "center", edit: "text" },
            { title: 'do', fixed: "right", toolbar: "#barToEdit", width: 90, align: "center" }
        ]]
    });

    //查询关键词操作
    form.on("submit(btnsearch)", function (data) {
        var data = data.field;
        tablekeywords.reload({
            where: { functionname: "jhy_wechat_keywords_query", kw_keyword: data.keywords },
        });
    });

    //创建关键词按钮
    $("#btnaddkeywords").click(function () {
        var formhtml = "<div style=\"padding:10px;\">\
                        <form class='layui-form' lay-filter='formaddkeyword'>\
                        <input type=\"text\" class=\"layui-input\" placeholder=\"输入关键字\"></br>\
                        <input type=\"text\" class=\"layui-input\" placeholder=\"输入关键字描述\"></br>\
                        <select >\
                            <option value='text'>文本</option>\
                            <option value='news'>图文</option>\
                            <option value='image'>图片</option>\
                            <option value='voice'>语音</option>\
                            <option value='video'>视频</option>\
                            <option value='music'>音乐</option>\
                        </select></br>\
                        <select >\
                            <option value='0'>可见</option>\
                            <option value='1'>隐藏</option>\
                        </select>\
                        </form></div>";
        layer.open({
            type: 1,
            title: "创建关键词",
            content: formhtml,
            btn: '添加',
            skin: "jhy-layer-css jhy-layer-css-select",
            success: function () {
                form.render(null, 'formaddkeyword');
            },
            yes: function (index, layero) {
                var kw_keyword = $.trim(layero.find("input:eq(0)").val());
                var kw_description = $.trim(layero.find("input:eq(1)").val());                
                var kw_msgtype = layero.find("select:eq(0)").val();
                var kw_visable = layero.find("select:eq(1)").val();
                if (kw_keyword == "") {
                    layer.msg("关键词不能为空");
                } else {
                    $.ajax({
                        type: "post",
                        url: "/Ashx/wechat.ashx",
                        data: { functionname: "jhy_wechat_keywords_add", kw_keyword: kw_keyword, kw_description: kw_description, kw_msgtype: kw_msgtype, kw_visable: kw_visable },
                        success: function (msg) {
                            if (msg == "true") {
                                layer.msg("成功");
                                tablekeywords.reload();
                                layer.close(index);
                            } else {
                                layer.msg("失败");
                                tablekeywords.reload();
                                layer.close(index);
                            }
                        }
                    });
                }
            }
        })

    });

    //修改关键词及排序
    table.on('edit(tablekeywords)', function (obj) {
        var kw_para = $.trim(obj.value),
            kw_field = obj.field,
            data = obj.data,
            kw_id = data.kw_id;
        if (kw_field == "kw_order") {
            if (isNaN(kw_para)) {
                layer.msg("只能数字");
                tablekeywords.reload();
                return false;
            }
        }
        if (kw_para == "" || kw_para == null) {
            layer.msg("总得写点啥吧");
            tablekeywords.reload();
            return false;
        }
        $.ajax({
            type: "post",
            url: "/Ashx/wechat.ashx",
            data: { functionname: "jhy_wechat_keywords_edit", kw_id: kw_id, kw_field: kw_field, kw_para: kw_para },
            success: function (msg) {
                if (msg == "true") {
                    layer.msg("成功");
                    tablekeywords.reload();
                } else {
                    layer.msg("失败");
                    tablekeywords.reload();
                }
            }
        });
    });

    //修改类型及是否可见状态
    table.on('tool(tablekeywords)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'msgtype') { //修改回复类型

            var formhtml = "<div style=\"padding:10px;\">\
                        <form class='layui-form' lay-filter='formaddkeyword'>\
                       <select >\
                            <option value='text'>文本</option>\
                            <option value='news'>图文</option>\
                            <option value='image'>图片</option>\
                            <option value='voice'>语音</option>\
                            <option value='video'>视频</option>\
                            <option value='music'>音乐</option>\
                        </select></br>\
                        </form></div>";
            layer.open({
                title: "修改回复类型",
                content: formhtml,
                type: 1,
                btn: '修改',
                skin: "jhy-layer-css jhy-layer-css-select",
                success: function (layero, index) {
                    layero.find("select").val(data.kw_msgtype);
                    form.render(null, 'formaddkeyword');
                }, yes: function (index, layero) {
                    var kw_para = layero.find("select").val(),
                        kw_id = data.kw_id,
                        kw_field = "kw_msgtype";
                    if (kw_para != data.kw_msgtype) {

                        $.ajax({
                            type: "post",
                            url: "/Ashx/wechat.ashx",
                            data: { functionname: "jhy_wechat_keywords_edit", kw_id: kw_id, kw_field: kw_field, kw_para: kw_para },
                            success: function (msg) {
                                if (msg == "true") {
                                    layer.msg("成功");
                                    obj.update({
                                        kw_msgtype: kw_para
                                    });
                                } else {
                                    layer.msg("失败");
                                    tablekeywords.reload();
                                }
                            }
                        });
                    }
                    layer.close(index);

                }
            });
        } else if (layEvent === 'visable') { //修改可见状态
            layer.confirm('修改关键词状态？', {
                btn: ['显示', '隐藏'] //按钮
            }, function () {
                var kw_id = data.kw_id,
                    kw_para = "0",
                    kw_field = "kw_visable";
                $.ajax({
                    type: "post",
                    url: "/Ashx/wechat.ashx",
                    data: { functionname: "jhy_wechat_keywords_edit", kw_id: kw_id, kw_field: kw_field, kw_para: kw_para },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.update({
                                kw_visable: kw_para
                            });
                        } else {
                            layer.msg("失败");
                            tablekeywords.reload();
                        }
                    }
                });
            }, function () {
                var kw_id = data.kw_id,
                    kw_para = "1",
                    kw_field = "kw_visable";
                $.ajax({
                    type: "post",
                    url: "/Ashx/wechat.ashx",
                    data: { functionname: "jhy_wechat_keywords_edit", kw_id: kw_id, kw_field: kw_field, kw_para: kw_para },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.update({
                                kw_visable: kw_para
                            });
                        } else {
                            layer.msg("失败");
                            tablekeywords.reload();
                        }
                    }
                });
            });
        } else if (layEvent === 'todelete') { //删除
            var kw_id = data.kw_id;
            layer.confirm('删除确认（内容一并删除）', function (index) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/wechat.ashx",
                    data: { functionname: "jhy_wechat_keywords_delete", kw_id: kw_id },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.msg("失败");
                            tablekeywords.reload();
                            layer.close(index);
                        }
                    }
                });
            });
        } else if (layEvent === 'toitem') { //进入内容页
            var tablehtml = "<div style='padding:10px;'>\
                           <table class=\"layui-table\" id=\"tablekeywordsitem\" lay-filter=\"tablekeywordsitem\" style='margin:0px;'></table>\
                           </div>"
            layeritem = layer.open({
                title: "关键词【" + data.kw_keyword + "】回复内容设置",
                type: 1,
                area: ["100%", "100%"],
                content: tablehtml,
                btn: ["添加内容"],
                success: function (layero, index) {
                    parentid = data.kw_id;
                    if (data.kw_msgtype == 'text') {
                        tablekeywordsitem = table.render({
                            elem: '#tablekeywordsitem',
                            url: '/Ashx/wechat.ashx', //数据接口
                            method: 'post',
                            where: { functionname: "jhy_wechat_keywords_item_query", kw_parentid: data.kw_id },
                            cols: [[ //表头                   
                                { field: 'kw_text_content', event:"kw_text_content", title: '文本内容', minWidth: 200, edit: "text", templet:"<div>{{unescape(d.kw_text_content)}}</div>" },
                                { field: 'kw_order', title: '排序', edit: "text", width: 60, align: 'center' },
                                { title: 'do', fixed: "right", templet: "<div> <a class=\"layui-btn layui-btn-xs\" lay-event=\"todelete\"><i class=\"layui-icon layui-icon-delete\"></i></a></div>", width: 56, align: 'center' }
                            ]]
                        });
                    } else if (data.kw_msgtype == "news") {
                        tablekeywordsitem = table.render({
                            elem: '#tablekeywordsitem',
                            url: '/Ashx/wechat.ashx', //数据接口
                            method: 'post',
                            where: { functionname: "jhy_wechat_keywords_item_query", kw_parentid: data.kw_id },
                            cols: [[ //表头                           
                                { field: 'kw_news_title', title: '标题', edit: "text" },
                                { field: 'kw_news_description', title: '描述', edit: "text" },
                                { field: 'kw_news_picurl', title: '图片链接', edit: "text" },
                                { field: 'kw_news_url', title: '内容链接', edit: "text" },
                                { field: 'kw_order', title: '排序', edit: "text", width: 60, align: 'center' },
                                { title: 'do', fixed: "right", templet: "<div> <a class=\"layui-btn layui-btn-xs\" lay-event=\"todelete\"><i class=\"layui-icon layui-icon-delete\"></i></a></div>", width: 56, align: 'center' }
                            ]]
                        });
                    }

                }, yes: function (index, layero) {
                    var kw_parentid = parentid,
                        kw_news_title = null,
                        kw_news_description = null,
                        kw_news_picurl = null,
                        kw_news_url = null,
                        kw_text_content = null;
                    if (data.kw_msgtype == 'text') {
                        layer.prompt({
                            formType: 0,
                            title: '添加回复内容'
                        }, function (value, indexadd, elem) {
                            kw_text_content = escape(value);
                            $.ajax({
                                type: "post",
                                url: "/Ashx/wechat.ashx",
                                data: { functionname: "jhy_wechat_keywords_item_add", kw_parentid: kw_parentid, kw_news_title: kw_news_title, kw_news_description: kw_news_description, kw_news_picurl: kw_news_picurl, kw_news_url: kw_news_url, kw_text_content: kw_text_content },
                                success: function (msg) {
                                    if (msg == "true") {
                                        layer.msg("成功");
                                        tablekeywordsitem.reload();
                                        layer.close(indexadd);
                                    } else {
                                        layer.msg("失败");
                                        tablekeywordsitem.reload();
                                        layer.close(indexadd);
                                    }
                                }
                            });
                        });
                    } else if (data.kw_msgtype == 'news') {
                        var layeradditem = layer.open({
                            title: "关键词【" + data.kw_keyword + "】添加内容",
                            type: 1,
                            area: ["100%", "100%"],
                            content: $("#diveditkeywordsitem"),
                            btn: ["添加"],
                            success: function () { },
                            yes: function (indexadd, layero) {
                                kw_news_title = $.trim(layero.find("#newstitle").val());
                                kw_news_description = $.trim(layero.find("#newsdescription").val());
                                kw_news_picurl = $.trim(layero.find("#newspicurl").val());
                                kw_news_url = $.trim(layero.find("#newsurl").val());
                                var ermsg = "";
                                if (kw_news_title == "") {
                                    ermsg += "标题不能为空<br>";
                                }
                                if (kw_news_description == "") {
                                    ermsg += "描述不能为空<br>";
                                }
                                if (kw_news_picurl == "") {
                                    ermsg += "图片不能为空<br>";
                                }
                                if (kw_news_url == "") {
                                    ermsg += "连接不能为空";
                                }
                                if (ermsg.length > 0) {
                                    layer.msg(ermsg);
                                    return false;
                                }
                                $.ajax({
                                    type: "post",
                                    url: "/Ashx/wechat.ashx",
                                    data: { functionname: "jhy_wechat_keywords_item_add", kw_parentid: kw_parentid, kw_news_title: kw_news_title, kw_news_description: kw_news_description, kw_news_picurl: kw_news_picurl, kw_news_url: kw_news_url, kw_text_content: kw_text_content },
                                    success: function (msg) {
                                        if (msg == "true") {
                                            layer.msg("成功");
                                            tablekeywordsitem.reload();
                                            layer.close(indexadd);
                                        } else {
                                            layer.msg("失败");
                                            tablekeywordsitem.reload();
                                            layer.close(indexadd);
                                        }
                                    }
                                });

                            }
                        });
                    } else if ('其他') {

                    }


                }
            });



        }
    });

    //修改子项内容
    table.on('edit(tablekeywordsitem)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
        var kw_para = obj.value,
            kw_field = obj.field,
            data = obj.data,
            kw_id = data.kw_id;
        if (kw_field == 'kw_text_content') {
            kw_para = escape(kw_para);
        }       
        $.ajax({
            type: "post",
            url: "/Ashx/wechat.ashx",
            data: { functionname: "jhy_wechat_keywords_item_edit", kw_id: kw_id, kw_field: kw_field, kw_para: kw_para },
            success: function (msg) {
                if (msg == "true") {
                    layer.msg("成功");
                    tablekeywordsitem.reload();
                } else {
                    layer.msg("失败");
                    tablekeywordsitem.reload();
                }
            }
        });

    });


    table.on('tool(tablekeywordsitem)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        var td = $(this);
        if (layEvent === 'todelete') { //删除
            layer.confirm('删除确认！', function (indexdelete) {
                $.ajax({
                    type: "post",
                    url: "/Ashx/wechat.ashx",
                    data: { functionname: "jhy_wechat_keywords_item_delete", kw_id: data.kw_id },
                    success: function (msg) {
                        if (msg == "true") {
                            layer.msg("成功");
                            obj.del();
                        } else {
                            layer.msg("失败");
                            tablekeywordsitem.reload();
                        }
                    }
                });
                layer.close(index);
            });   

        } else if (layEvent === "kw_text_content") {
            td.find("input").val(unescape(data.kw_text_content));
        }



    });
    


});